#!/bin/bash
###
 # @Author: Logan.Li
 # @Gitee: https://gitee.com/attacker
 # @email: admin@attacker.club
 # @Date: 2025-02-25 20:11:17
 # @LastEditTime: 2025-02-25 20:11:24
 # @Description: 
### 
set -e

# 确保运行脚本的用户是root
if [ "$EUID" -ne 0 ]; then
    echo "[ERROR] 请使用root用户运行此脚本"
    exit 1
fi

# 下载RocketMQ Dashboard
echo "[INFO] 正在下载RocketMQ Dashboard..."
mkdir -p /opt/rocketmq-dashboard
cd /opt/rocketmq-dashboard
wget "http://oss.wuyou.run/software/rocketmq/rocketmq-dashboard-1.0.0.jar" || { echo "[ERROR] 下载失败"; exit 1; }

# 创建rocketmq-dashboard.service
echo "[INFO] 正在创建RocketMQ Dashboard服务..."
cat <<EOF | tee /etc/systemd/system/rocketmq-dashboard.service > /dev/null
[Unit]
Description=RocketMQ Dashboard
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/opt/rocketmq-dashboard
ExecStart=/usr/bin/nohup /usr/bin/java -jar /opt/rocketmq-dashboard/rocketmq-dashboard-1.0.0.jar
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

# 重新加载systemd配置并启用服务
echo "[INFO] 正在启用RocketMQ Dashboard服务..."
systemctl daemon-reload
systemctl enable rocketmq-dashboard.service

# 启动RocketMQ Dashboard服务
echo "[INFO] 正在启动RocketMQ Dashboard服务..."
systemctl start rocketmq-dashboard.service

echo "RocketMQ Dashboard已成功部署并且设置了开机启动。可以通过 'journalctl -u rocketmq-dashboard.service' 查看日志。"